package com.bw.dao;

import com.bw.pojo.Goods;
import com.bw.util.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***************************
 FileAction: 提供商品的增删改查

 CreateTime:2022-07-29 15:40
 Author:Asking-mac
 ****************************/
public class GoodsDao {

    public void insertGoods(Goods goods) throws SQLException {
        // 1. sql语句
        String sql = "insert into goods values(null,?,?,?,?,?)";
        Connection conn = JDBC.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setObject(1,goods.getGoods_name());
        ps.setObject(2,goods.getGoods_price());
        ps.setObject(3,goods.getGoods_stock());
        ps.setObject(4,goods.getGoods_des());
        ps.setObject(5,goods.getGoods_up_time());

        ps.executeUpdate();
    }


    public List<Goods> selectAllGoods() throws SQLException {
        // 1. sql语句
        String sql = "select * from goods";
        Connection conn = JDBC.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();
        List<Goods> list = new ArrayList<>();

        while(rs.next()){
            Goods goods = new Goods(rs.getInt(1),
                    rs.getString(2),
                    rs.getDouble(3),
                    rs.getInt(4),
                    rs.getString(5),
                    rs.getString(6));

            list.add(goods);
        }

        return list;
    }

    public List<Goods> selectAllGoodsOrderPrice() throws SQLException {
        // 1. sql语句
        String sql = "select * from goods order by goods_price desc";
        Connection conn = JDBC.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();
        List<Goods> list = new ArrayList<>();

        while(rs.next()){
            Goods goods = new Goods(rs.getInt(1),
                    rs.getString(2),
                    rs.getDouble(3),
                    rs.getInt(4),
                    rs.getString(5),
                    rs.getString(6));

            list.add(goods);
        }

        return list;
    }


}
